package cn.yuxiaoyuan.controller;

import cn.yuxiaoyuan.common.authorize.PreAuthorize;
import cn.yuxiaoyuan.common.util.R;
import cn.yuxiaoyuan.service.IEmployeeService;
import cn.yuxiaoyuan.system.domain.Employee;
import cn.yuxiaoyuan.system.query.EmployeeQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author solargen
 * @version 1.0
 * @date 2025/5/11 10:37
 */
@Api(value = "员工相关的接口")
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @PreAuthorize(sn = "employee:findById",name = "根据id查询员工",descs = "根据id查询员工")
    @GetMapping("/{id}")
    public R findById(@PathVariable("id")Long id){
        return R.ok(employeeService.findById(id));
    }

    @ApiOperation(value = "查询全部员工信息")
    @GetMapping
    @PreAuthorize(sn = "employee:findAll",name = "查询全部员工",descs = "查询全部员工")
    public R findAll(){
        return R.ok(employeeService.findAll());
    }

    @DeleteMapping("/{id}")
    @PreAuthorize(sn = "employee:deleteById",name = "根据id删除员工",descs = "根据id删除员工")
    public R deleteById(@PathVariable("id")Long id){
        employeeService.deleteById(id);
        return R.ok();
    }
    @PostMapping("/deleteAll")
    @PreAuthorize(sn = "employee:batchDelete",name = "根据ids批量删除员工",descs = "根据ids批量删除员工")
    public R batchDelete(@RequestBody List<Long> ids){
        employeeService.batchDelete(ids);
        return R.ok();
    }

    @PostMapping
    @PreAuthorize(sn = "employee:add",name = "添加或修改员工",descs = "添加或修改员工")
    public R add(@RequestBody Employee employee){
        if(employee.getId() == null){
            employeeService.add(employee);
        }else {
            employeeService.update(employee);
        }
        return R.ok();
    }

    @PostMapping("/list")
    @PreAuthorize(sn = "employee:pageList",name = "员工的分页查询",descs = "员工的分页查询")
    public R pageList(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.pageList(employeeQuery);
    }
}
